from selenium import webdriver
import time
# chromedriver 配置相关的
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# 定时延迟的
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

# 初始化 Chrome 选项
chrome_options = Options()

# 添加任何需要的选项
# chrome_options.add_argument("--headless")  # 无头模式
# chrome_options.add_argument("--disable-gpu")  # 禁用 GPU

# 设置 ChromeDriver 的路径
service = Service(executable_path='E:\\PythonGit\\Python_Crawler\\learn\\chromedriver\\chromedriver.exe')

# 创建 WebDriver 实例
driver = webdriver.Chrome(service=service, options=chrome_options)

# 访问登录页面
driver.get("https://yueshushu.top/Stock/login")

# 获取 capabilities
capabilities = driver.capabilities

# 访问 capabilities 中的 browserName
browser_name = capabilities.get("browserName")

print(f"Browser Name: {browser_name}")

# 等待用户名输入框出现
username_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "account"))
)

# 输入用户名
username_input.clear()
username_input.send_keys("yjl")

# 等待密码输入框出现
password_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "password"))
)

# 输入密码
password_input.clear()
password_input.send_keys("123456")

# 等待登录按钮出现
login_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "myLogin"))
)
print('开始点击登录按钮')
# 点击登录按钮
login_button.click()

# 等待登录成功后的页面元素出现
try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "app"))
    )
    print(f"Login successful! Message:")
except Exception as e:
    print(f"Login failed: {e}")
input('按任意键结束')
# 关闭浏览器
driver.quit()